package net.stuffrepos.bricknetcenter.http.util;

import java.util.List;
import net.stuffrepos.bricknetcenter.http.HttpParser;
import net.stuffrepos.bricknetcenter.http.HttpResponse;
import org.jaxen.jdom.JDOMXPath;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;

/**
 *
 * @author Eduardo H. Bogoni <eduardobogoni@gmail.com>
 */
public abstract class ParserHelper implements HttpParser {

    public abstract String getXPath();

    public abstract void parseAttributeValue(String attributeValue) throws Exception;

    @Override
    public void parse(HttpResponse response) throws Exception {
        SAXBuilder builder = new SAXBuilder(org.ccil.cowan.tagsoup.Parser.class.getName());
        Document document = builder.build(response.getInputStream());
        JDOMXPath xpath = new JDOMXPath(getXPath());
        xpath.addNamespace("h", "http://www.w3.org/1999/xhtml");

        for (Attribute a : (List<Attribute>) xpath.selectNodes(document)) {
            parseAttributeValue(a.getValue());
        }
    }
}
